﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CommonFunc;
using DAL = AccessDAL;

public partial class Manageweb_ManageINews_AddINews : System.Web.UI.Page
{
    DAL.INewsClassTableAdapters.INewsClassTableAdapter DINC =
        new DAL.INewsClassTableAdapters.INewsClassTableAdapter();

    MODEL.INews MIN = new MODEL.INews();
    MODEL.INews _MIN = new MODEL.INews();
    MODEL.INewsClass MINC = new MODEL.INewsClass();
    MODEL.INewsClass _MINC = new MODEL.INewsClass();

    BLL.INews BIN = new BLL.INews();
    BLL.INewsClass BINC = new BLL.INewsClass();


    protected delegate void JumpPage(string _PageName, Page _Page);
    JumpPage JP = new JumpPage(CommonFunction.JumpAnotherPage);


    protected delegate void UploadThumbnail(FileUpload _FU, Label _LB, int Thumbwidth, string _Prefix);
    UploadThumbnail UploadThumb = new UploadThumbnail(AutoThumb.UploadThumbnail);


    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.Cookies["MasterID"] == null)
        {
            Response.Redirect("../Default.aspx");
        }

        if (Request.QueryString["INewsID"] != null)
        {
            LB_ID.Text = Request.QueryString["INewsID"].ToString();
        }

        if (!IsPostBack)
        {
            DDL_ClassDataBind();

            PageDataInit();
        }

        ClickInit();

        if (Request.QueryString["ClassID"] != null)
        {
            DDL_Class.SelectedValue = Request.QueryString["ClassID"].ToString();
        }
    }


    #region ClickInit [初始化]
    protected void ClickInit()
    {
        LB_AddINews.Text = "";

        try
        {
            if (LB_ID.Text == "")
            {
                Btn_Submit.Click += new EventHandler(Btn_Add_Click);
                LB_TableTitle.Text = "添加图文";
                TABLE_ModImage.Visible = false;
            }
            else
            {
                Btn_Submit.Click += new EventHandler(Btn_Mod_Click);
                LB_TableTitle.Text = "修改图文";
                TABLE_ModImage.Visible = true;
            }
        }
        catch (Exception ex)
        {
            LB_AddINews.Text = ex.Message;
        }
    }
    #endregion


    #region 页面数据 [初始化]
    public void PageDataInit()
    {
        try
        {
            if (LB_ID.Text == "")
            {
                TB_Title.Text = "";
                DDL_Class.SelectedValue = "";
                FU_Thumb.Visible = true;
                IMG_Thumb.Visible = false;
                CB_IsLock.Checked = false;
                CB_Index.Checked = false;
                FTB_Content.Text = "";
            }
            else
            {
                MIN.IN_ID = Convert.ToInt32(LB_ID.Text);

                _MIN = BIN.GetDataByID(MIN);

                TB_Title.Text = _MIN.IN_Title;
                DDL_Class.SelectedValue = _MIN.IN_ClassID.ToString();
                FU_Thumb.Visible = false;
                IMG_Thumb.Visible = true;
                if (_MIN.IN_SmallThumb == "")
                {
                    IMG_Thumb.ImageUrl = "~/Manageweb/images/null.jpg";
                }
                else
                {
                    IMG_Thumb.ImageUrl = "~/UploadImage/" + _MIN.IN_SmallThumb;
                }
                CB_IsLock.Checked = _MIN.IN_IsLock;
                CB_Index.Checked = _MIN.IN_Index;
                FTB_Content.Text = _MIN.IN_Content;
            }
        }
        catch (Exception ex)
        {
            LB_AddINews.Text = ex.Message;
        }
    }
    #endregion


    #region DDL_Class数据绑定 [初始化]
    protected void DDL_ClassDataBind()
    {
        try
        {
            DDL_Class.Items.Clear();

            ListItem LI0 = new ListItem("", "");
            DDL_Class.Items.Add(LI0);

            DataTable DT = DINC.GetAllDataOrderByClassCode();

            foreach (DataRow dr in DT.Rows)
            {
                ListItem li = new ListItem(
                    ClassCode.CreateClassListItemText(dr["INC_Title"].ToString(), dr["INC_Level"].ToString()),
                    dr["INC_ID"].ToString());

                DDL_Class.Items.Add(li);
            }
        }
        catch (Exception ex)
        {
            LB_AddINews.Text = ex.Message;
        }
    }
    #endregion


    #region 类别标题 [格式化]
    protected string FormatClassTitle(object _Title, object _Level)
    {
        return ClassCode.CreateClassListItemText(_Title.ToString(), _Level.ToString());
    }
    #endregion


    #region 添加图文 [Click]
    protected void Btn_Add_Click(object sender, EventArgs e)
    {
        try
        {
            if (FU_Thumb.HasFile)
            {
                UploadThumb(FU_Thumb, LB_SmallThumb, 160, "th_INews_Small_");
                UploadThumb(FU_Thumb, LB_Thumb, 480, "th_INews_big_");

                MIN.IN_Thumbnail = LB_Thumb.Text;
                MIN.IN_SmallThumb = LB_SmallThumb.Text;
            }
            else
            {
                MIN.IN_Thumbnail = "";
                MIN.IN_SmallThumb = "";
            }

            MIN.IN_Title = TB_Title.Text;
            MIN.IN_ClassID = Convert.ToInt32(DDL_Class.SelectedValue);
            MIN.IN_Content = FTB_Content.Text;
            MIN.IN_Hits = 0;
            MIN.IN_MasterID = Convert.ToInt32(Session["MasterID"]);
            MIN.IN_DateTime = DateTime.Now;
            MIN.IN_IsLock = CB_IsLock.Checked;
            MIN.IN_IsDel = false;
            MIN.IN_Index = CB_Index.Checked;

            BIN.Add(MIN);

            LB_AddINews.Text = "添加成功，页面即将自动跳转";

            MINC.INC_ID = MIN.IN_ClassID;

            _MINC = BINC.GetDataByID(MINC);

            if (_MINC.INC_IsLock)
            {
                JP("ManageINews.aspx?ClassID=" + _MINC.INC_ID, this.Page);
            }
            else
            {
                JP("ManageINews.aspx", this.Page);
            }
        }
        catch (Exception ex)
        {
            LB_AddINews.Text = ex.Message;
        }
    }
    #endregion


    #region 修改图文 [Click]
    protected void Btn_Mod_Click(object sender, EventArgs e)
    {
        try
        {
            MIN.IN_Title = TB_Title.Text;
            MIN.IN_ClassID = Convert.ToInt32(DDL_Class.SelectedValue);
            MIN.IN_Content = FTB_Content.Text;
            MIN.IN_MasterID = Convert.ToInt32(Session["MasterID"]);
            MIN.IN_IsLock = CB_IsLock.Checked;
            MIN.IN_Index = CB_Index.Checked;
            MIN.IN_ID = Convert.ToInt32(LB_ID.Text);

            BIN.Mod(MIN);

            LB_AddINews.Text = "修改成功，页面即将自动跳转";

            JP("ManageINews.aspx", this.Page);
        }
        catch (Exception ex)
        {
            LB_AddINews.Text = ex.Message;
        }
    }
    #endregion


    #region 修改图片 [Click]
    protected void Btn_ModImage_Click(object sender, EventArgs e)
    {
        try
        {
            MIN.IN_ID = Convert.ToInt32(LB_ID.Text);

            UploadThumb(FU_ModImage, LB_SmallThumb, 160, "th_INews_Small_");
            UploadThumb(FU_ModImage, LB_Thumb, 480, "th_INews_big_");

            MIN.IN_Thumbnail = LB_Thumb.Text;
            MIN.IN_SmallThumb = LB_SmallThumb.Text;

            BIN.ModImage(MIN);

            LB_AddINews.Text = "修改成功";

            PageDataInit();
        }
        catch (Exception ex)
        {
            LB_AddINews.Text = ex.Message;
        }
    }
    #endregion
}
